<?php declare(strict_types=1);


namespace PinWuYun\domain\service\commonServer;

use Exception;
use GuzzleHttp\Exception\GuzzleException;
use PinWuYun\domain\helper\GoodsUpdateHelper;
use PinWuYun\domain\helper\syncHelper;
use PinWuYun\domain\service\starLinkServer\Goods;
use PinWuYun\domain\service\starLinkServer\StarDataServer;
use PinWuYun\domain\service\youZanServer\GoodsServer;
use PinWuYun\domain\service\youZanServer\LogServer;
use PinWuYun\domain\service\youZanServer\TokenServer;

/**
 * Created: by zhiJie
 * Date: 2021/6/24
 */
class SyncService
{

    private $starGoodsService;
    // 初始化类
    public function __construct()
    {
        $this->starGoodsService = new Goods();
    }

    /**
     * @throws GuzzleException
     * @throws Exception
     */
    public function syncGoodsAll(): string
    {
        /** 获取全部商品spuId ，先拟定一条  */
//         $spuList = (new StarDataServer)->getAllSpuId();
        $spuList = [126509];
        /**  获取星链商品数据 */
        $accessToken = TokenServer::getAccessToken();
        $spuIdChunk = array_chunk($spuList, 50);
        $starGoodsService = $this->starGoodsService;
        foreach ($spuIdChunk as $iValue) {
            $spuChunk = implode(',', $iValue);
            $spuDetailList = json_decode($starGoodsService->getSpuDetail(['spuIds' => $spuChunk])->getBody()->getContents(), true)['data'] ?? [];
            foreach ($spuDetailList as $spu) {
                $this->syncGoods($spu, $accessToken);
            }
        }
        return 'success';
    }


    /**
     * 更新商品
     * @throws GuzzleException
     * @throws Exception
     */
    public function updateGoodsAll(): string
    {
        // 前一天到当前时间 所有更新记录
        $changeRecord = (new StarDataServer)->getAllChangeRecord();
        if(!$changeRecord) {
            echo '无更新记录，定时任务结束';
            exit();
        }
        $access_token = TokenServer::getAccessToken();
        $changeHelper = new GoodsUpdateHelper();
        foreach ($changeRecord as $item) {
            $action = $changeHelper->getHandleAction($item['recordType']);
            // 获取方法，然后执行，这里idea无法跳转，可到 GoodsUpdateHelper 查看具体实现
            $changeHelper->$action($item['spuId'], $access_token);
        }
        return 'success';
    }




    /** --------------------------------------------------------------------- */
    /**
     * 同步商品数据到有赞,一个spu
     * @param array $spuDetail
     * @param string $accessToken
     * @throws GuzzleException
     * @throws Exception
     */
    public function syncGoods(array &$spuDetail, string $accessToken)
    {
        /** 获取星链商品数据详情*/
        $spuDetail['skuList'] = (new StarDataServer)->getAllInfoWithSpuId($spuDetail['spuId']);
        /** 将星链数据保存到有赞 */
        $res = (new GoodsServer)->addGoods($spuDetail, $accessToken);
        if($res['code'] === 200) {
            // 保存别名和唯一id
            syncHelper::syncGoodsToDb($spuDetail['spuId'], $res['data']['alias'], $res['data']['item_id']);
        }
        /** 如果有视频文件，将视频文件位置保存起来 */
        if($spuDetail['videoUrl']) {
            LogServer::saveStarVideoLog($spuDetail['name'],$spuDetail['videoUrl']);
        }
    }
        
}